package com.example.controller;

import cn.dev33.satoken.util.SaResult;
import com.example.utils.CommonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/upload")
@Tag(name = "文件上传", description = "上传文件到服务器")
public class FileUploadController {

	@Autowired
	private CommonUtil commonUtil;

	/**
	 * 上传文件接口
	 *
	 * @param file    文件
	 * @param subPath 子路径
	 * @return 文件在服务器的路径
	 */
	@Operation(summary = "上传文件")
	@PostMapping
	@Parameters({
			@Parameter(name = "file", description = "文件", required = true),
			@Parameter(name = "subPath", description = "子路径", in = ParameterIn.QUERY)
	})
	public SaResult handleFileUpload(@RequestParam("file") MultipartFile file,
									 @RequestParam("subPath") String subPath) {
		log.info("收到文件:{},大小:{},子路径:{}", file.getOriginalFilename(), file.getSize(), subPath);
		Map<String, String> stringStringMap = commonUtil.uploadFile(file, subPath);
		return SaResult.ok("上传成功").setData(stringStringMap);
	}
}
